package com.study.demo.aop.demo1_2.config;

import com.study.demo.aop.demo1_2.interceptor.BeanFactoryLogOperationSourceAdvisor;
import com.study.demo.aop.demo1_2.interceptor.LogInterceptor;
import com.study.demo.aop.demo1_2.operation.AnnotationLogOperationSource;
import com.study.demo.aop.demo1_2.operation.LogOperationSource;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;

/**
 * @author: elvin
 * @time: 2019-10-16 13:23
 * @desc:
 **/
@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class ProxyLogConfiguration extends AbstractLogConfiguration {

    //代理通知者,
    // 1.需要知道那些类需要被代理, 即要有查找他们的方法 -->
    // 2.需要有处理代理方法的 invoke 方法
    @Bean(name = "org.springframework.cache.config.internalLogAdvisor")
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public BeanFactoryLogOperationSourceAdvisor cacheAdvisor() {
        BeanFactoryLogOperationSourceAdvisor advisor =
                new BeanFactoryLogOperationSourceAdvisor();
        advisor.setLogOperationSource(logOperationSource());
        advisor.setAdvice(logInterceptor());
        if (this.enableLogging != null) {
            advisor.setOrder(this.enableLogging.<Integer>getNumber("order"));
        }
        return advisor;
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public LogOperationSource logOperationSource() {
        return new AnnotationLogOperationSource();
    }


    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public LogInterceptor logInterceptor() {
        LogInterceptor interceptor = new LogInterceptor();
        interceptor.setLogOperationSource(logOperationSource());
        return interceptor;
    }
}
